package com.guochao.controller;

import com.guochao.entity.Log;
import com.guochao.entity.Product;
import com.guochao.entity.ShoppingCart;
import com.guochao.service.LoginService;
import com.guochao.service.ProductService;
import com.guochao.service.ShoCartService;
import com.guochao.util.ToolsUtil;
import com.guochao.util.UserUtil;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/usercart")
public class ShoCartController {

    @Resource
    private ShoCartService shoCartSvc;
    @Resource
    private ProductService productSvc;
    @Resource
    private LoginService loginSvc;

    @RequestMapping(value = "addcart")
    public String addCart(ShoppingCart cart) {
        System.out.println("cart34"+cart);
        //检查购物车内是否有该物品
        if (shoCartSvc.FindCartByID(cart.getUid(),cart.getPid(),cart.getSize())==null){

        cart.setCartbytime(ToolsUtil.getTime("yyyy-MM-dd HH:mm:ss", 0));

        Product product = productSvc.proSelByCId(cart.getPid());

        cart.setPriceall(cart.getNumbers() * product.getPrice());


        final int i = shoCartSvc.addCart(cart);
        Log log = ToolsUtil.insertLog(UserUtil.getLoginUserId(), "您添加了一款名为【" + product.getPname() + "】的商品到购物车");
        loginSvc.insertUserLog(log);}
        //若该商品已被添加购物车则增加购物车中该商品数量
        else if (shoCartSvc.FindCartByID(cart.getUid(),cart.getPid(),cart.getSize())!=null){
            ShoppingCart cart1=shoCartSvc.FindCartByID(cart.getUid(),cart.getPid(),cart.getSize());
            cart1.setNumbers(cart.getNumbers()+cart1.getNumbers());
            shoCartSvc.updateCart(cart1);
        }
        return "redirect:mycart";
    }

    @RequestMapping(value = "delcart")
    public String delCart(Integer cartid) {
        int i = shoCartSvc.delCartByID(cartid);
        Log log = ToolsUtil.insertLog(UserUtil.getLoginUserId(), "您在购物车删除了购物ID为【" + cartid + "】的商品");
        loginSvc.insertUserLog(log);
        return "redirect:mycart";
    }

    /**
     * 批量删除购物车商品
     * */
    @ResponseBody
    @RequestMapping(value = "/delCartbyids",produces = "application/json;charset=UTF-8")
    public Object delCartByIds(@RequestParam("cartids[]") Integer[] cartids) {
        System.out.println("123123"+cartids+"123123");
        JSONObject object=new JSONObject();
        int i = shoCartSvc.delCartByIds(cartids);
        if(i>0){
            String descb = "您删除了【" + i + "】条购物车记录";
            Log log = ToolsUtil.insertLog(UserUtil.getLoginUserId(), descb);
            loginSvc.insertUserLog(log);
            object.put("my_i", i);
            System.out.println();
        }
        return object.toString();
    }

    @RequestMapping(value = "mycart")
    public ModelAndView myCart(ModelAndView view) {
        List<ShoppingCart> list = shoCartSvc.selAllCart(UserUtil.getLoginUserId());
        view.addObject("res_list", list);
        view.setViewName("user_cart");
        return view;
    }
}
